package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022-12-01 1:49
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 列表搜索
     *
     * @param searchParam
     * @return
     */
    @GetMapping("list.html")
    public String list(SearchParam searchParam, Model model) {
        Result<Map> result = listFeignClient.list(searchParam);
        model.addAllAttributes(result.getData());
        // 封装一个原本的url，拼接出原本的url
        String urlParam = this.makeUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);

        // 处理面包屑--品牌
        String trademarkParam = this.makeTrademarkParam(searchParam.getTrademark());
        model.addAttribute("trademarkParam", trademarkParam);
        // 处理面包屑--平台属性
        List<Map<String, String>> propsParamList = this.makePropsParamList(searchParam.getProps());
        model.addAttribute("propsParamList", propsParamList);

        // 处理排序
        Map<String, String> orderMap = this.makeOrderMap(searchParam.getOrder());
        model.addAttribute("orderMap", orderMap);
        return "list/index";
    }

    /**
     * 排序处理
     * @param order
     * @return
     */
    private Map<String, String> makeOrderMap(String order) {
        // order=1:desc
        // 1:hotScore 2:price
        // 默认是综合排序--热度排序
        // 创建返回的map
        Map<String ,String> resultMap = new HashMap<>();
        if(!StringUtils.isEmpty(order)) {
            // 处理两个属性 type sort
            String[] split = order.split(":");
            if(split != null && split.length == 2){
                resultMap.put("type",split[0]);
                resultMap.put("sort",split[1]);
            }
        }else {
            // 默认是综合排序
            resultMap.put("type", "1");
            resultMap.put("sort", "desc");
        }
        return resultMap;
    }

    /**
     * 处理面包屑--平台属性
     * 运行内存：4G
     * @param props
     * @return
     */
    private List<Map<String, String>> makePropsParamList(String[] props) {
        // 创建一个返回的集合
        List<Map<String, String>> resultList = new ArrayList<>();
        if(props != null && props.length > 0) {
            for (String prop : props) {
                // props=23:64G:机身内存
                String[] split = prop.split(":");
                if(split != null && split.length == 3) {
                    Map<String, String> map = new HashMap<>();
                    map.put("attrId", split[0]);
                    map.put("attrName", split[2]);
                    map.put("attrValue", split[1]);
                    resultList.add(map);
                }
            }
        }
        return resultList;
    }

    /**
     * 拼接面包屑 -- 品牌
     * 品牌：小米 ×
     * @param trademark
     * @return
     */
    private String makeTrademarkParam(String trademark) {
        if(!StringUtils.isEmpty(trademark)) {
            // 分离 1:小米
            String[] split = trademark.split(":");
            if(split != null && split.length == 2) {
                return "品牌：" + split[1];
            }
        }
        return "";
    }

    /**
     * 拼接UrlPram
     * list.html?category3Id=61&props=23:64G:机身内存
     *
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        // 拼接路径
        StringBuilder builder = new StringBuilder();
        // 入口一：关键字匹配查询
        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            builder.append("keyword=").append(searchParam.getKeyword());
        }
        // 入口二：分类入口
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())) {
            builder.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())) {
            builder.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())) {
            builder.append("category3Id=").append(searchParam.getCategory3Id());
        }
        // 入口结束之后,拼接品牌
        // &trademark=2:华为
        if(!StringUtils.isEmpty(searchParam.getTrademark())) {
            if(builder.length() > 0) {
                builder.append("&trademark=").append(searchParam.getTrademark());
            }
        }
        // 拼接平台属性
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            // 循环遍历
            for (String prop : props) {
                //&props=23:64G:机身内存
                if(builder.length() > 0) {
                    builder.append("&props=").append(prop);
                }
            }
        }
            return "list.html?" + builder.toString();
    }
}
